package com.kuang.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.SimpleDateFormat;

public class JsonUtils {

    public static String getJson(Object object){
        return  getJson(object,"yyyy-MM-dd HH:mm:ss");
    }

    public static String getJson(Object object , String dateFormat){
        ObjectMapper mapper = new ObjectMapper();
        //使用ObjectMapper来解决 b不使用时间戳方式
        mapper.configure(SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS,false);
        //传统方式
        SimpleDateFormat sdf  = new SimpleDateFormat(dateFormat);
        mapper.setDateFormat(sdf);

        try {
            return  mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return  null;

    }

}
